package com.ts.api.http.demo;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.encode.HexUtils;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.List;
import java.util.Map;

/**
 *  机构掩码测试
 *
 *  @author Pan
 */
@ApiPlan(productId = "1000034", type = ApiPlanConvertType.ORG_PHONE_MASK)
public class DemoApiOrgPhoneMask implements ApiOrgPhoneMaskConvertHandler {

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        String phoneMask = apiDataExchangeParameter.getPhoneMask();

        Map<String, Object> dataMap = MapUtils.newHashMap(newMap -> {
            newMap.put("phoneMask", phoneMask);
            newMap.put("reqId", apiDataExchangeParameter.getReqId());
        });

        AES ecb = AES.createEcb(HexUtils.decode("4987cd682c336e06142f2eb0722dfc05e5741c42df6171067c7c118d297743cc"));
        String data = ecb.encryptToHex(JsonUtils.toJson(dataMap));

        return MapUtils.newHashMap(newMap -> newMap.put("data", data));
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 1000)) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }

        RespOrgPhoneMaskDataExchange respOrgPhoneMaskDataExchange = RespOrgPhoneMaskDataExchange.ofSuc();

        JSONObject data = jsonObject.getJSONObject("data");

        String listPhoneMaskJson = data.getString("listPhoneMd5");
        List<String> listPhoneMask = JSONObject.parseArray(listPhoneMaskJson, String.class);
        respOrgPhoneMaskDataExchange.setListPhoneMd5(listPhoneMask);
        return respOrgPhoneMaskDataExchange;
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {

        Map<String, Object> dataMap = MapUtils.newHashMap(newMap -> {
            newMap.put("phone", apiApplyParameter.getPhone());
            newMap.put("reqId", apiApplyParameter.getReqId());
        });

        AES ecb = AES.createEcb(HexUtils.decode("4987cd682c336e06142f2eb0722dfc05e5741c42df6171067c7c118d297743cc"));
        String data = ecb.encryptToHex(JsonUtils.toJson(dataMap));

        return MapUtils.newHashMap(newMap -> newMap.put("data", data));
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 1000)) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}
